package com.wk.oms.service.impl;

import com.alibaba.fastjson.JSON;
import com.wk.common.constant.mq.ItemExchange;
import com.wk.common.constant.mq.ItemRoutingKey;
import com.wk.oms.entity.Order;
import com.wk.oms.entity.OrderItem;
import com.wk.oms.mapper.OrderItemMapper;
import com.wk.oms.mapper.OrderMapper;
import com.wk.oms.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wk.oms.vo.OrderSubmitVO;
import com.wk.pms.entity.SkuInfo;
import com.wk.pms.entity.SpuInfo;
import com.wk.pms.feign.SkuInfoFeign;
import com.wk.pms.feign.SpuInfoFeign;
import com.wk.ums.entity.Member;
import com.wk.ums.entity.MemberReceiveAddress;
import com.wk.ums.feign.MemberFeign;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private MemberFeign memberFeign;

    @Autowired
    private SpuInfoFeign spuInfoFeign;

    @Autowired
    private SkuInfoFeign skuInfoFeign;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Transactional
    @Override
    public Order saveOrder(OrderSubmitVO orderSubmitVO) {

        //收货地址
        MemberReceiveAddress address = orderSubmitVO.getAddress();

        //用户信息
        Member member = memberFeign.info(orderSubmitVO.getUserId()).getData();

        //当前时间
        LocalDateTime date = LocalDateTime.now();

        //订单编号 唯一标识
        String orderToken = orderSubmitVO.getOrderToken();

//-----------------------------------保存数据到订单表-----------------------------------------
        Order order = new Order();

        order.setMemberId(member.getId());
        order.setMemberUsername(member.getUsername());
        order.setOrderSn(orderToken);           //订单编号
        order.setCreateTime(date);              //订单创建时间
        order.setModifyTime(date);              //订单修改时间
        order.setTotalAmount(orderSubmitVO.getTotalPrice());    //订单总额
        order.setPayType(orderSubmitVO.getPayType());   //支付方式
        order.setSourceType(1);                 //订单来源 web端
        order.setStatus(0);                     //订单状态 0:待支付
        order.setDeliveryCompany(orderSubmitVO.getDeliveryCompany());       //物流选择
        order.setIntegration(10);               //商品积分
        order.setGrowth(10);                    //成长值
        order.setConfirmStatus(0);              //收货状态
        order.setDeleteStatus(0);               //订单删除状态

        //收货地址
        order.setReceiverName(address.getName());
        order.setReceiverPhone(address.getPhone());
        order.setReceiverPostCode(address.getPostCode());
        order.setReceiverProvince(address.getProvince());
        order.setReceiverCity(address.getCity());
        order.setReceiverRegion(address.getRegion());
        order.setReceiverDetailAddress(address.getDetailAddress());

        baseMapper.insert(order);

//-----------------------------------保存数据到订单商品表-----------------------------------------
        orderSubmitVO.getItems().forEach(item->{
            SkuInfo sku = skuInfoFeign.info(item.getSkuId()).getData();
            SpuInfo spu = spuInfoFeign.querySpuById(sku.getSpuId()).getData();

            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(order.getId());
            orderItem.setOrderSn(orderToken);
            orderItem.setSpuId(spu.getId());
            orderItem.setSpuName(spu.getSpuName());
            orderItem.setCategoryId(spu.getCatalogId());
            orderItem.setSkuId(item.getSkuId());
            orderItem.setSkuName(sku.getSkuName());
            orderItem.setSkuPic(sku.getSkuDefaultImg());
            orderItem.setSkuPrice(sku.getPrice());
            orderItem.setSkuQuantity(item.getCount());
            orderItem.setSkuAttrsVals(JSON.toJSONString(item.getSaleAttrValues()));

            orderItemMapper.insert(orderItem);
        });

        /*测试库存锁定 订单创建失败的情况
        int i = 1/0;*/

        //订单创建成功后发送订单编号到队列 有效期30分钟 如超过30分钟没有被消费 消息转入死信队列 OMS监听死信队列 关闭超时未付款的订单
        amqpTemplate.convertAndSend(ItemExchange.ORDER_EXCHANGE, ItemRoutingKey.ORDER_TTL,orderToken);

        return order;
    }
}
